package cn.jtcoding.item.service.impl;

import cn.jtcoding.common.framework.pagehelper.PagedGridResult;
import cn.jtcoding.common.framework.utils.DesensitizationUtil;
import cn.jtcoding.item.api.ItemService;
import cn.jtcoding.item.mapper.*;
import cn.jtcoding.item.pojo.dataobject.ItemsDO;
import cn.jtcoding.item.pojo.dataobject.ItemsImgDO;
import cn.jtcoding.item.pojo.dataobject.ItemsParamDO;
import cn.jtcoding.item.pojo.dataobject.ItemsSpecDO;
import cn.jtcoding.item.pojo.vo.ItemCommentLevelCountVO;
import cn.jtcoding.item.pojo.vo.ItemCommentVO;
import cn.jtcoding.item.pojo.vo.ItemInfoVO;
import cn.jtcoding.item.pojo.vo.ItemSearchVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jason.tang
 * @date 2021/3/10 12:22
 * @desc
 */

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemsMapper itemsMapper;
    @Autowired
    private ItemsImgMapper itemsImgMapper;
    @Autowired
    private ItemsParamMapper itemsParamMapper;
    @Autowired
    private ItemsSpecMapper itemsSpecMapper;
    @Autowired
    private ItemsCommentsMapper itemsCommentsMapper;

    @Override
    public ItemInfoVO getItemInfo(String itemId) {
        ItemsDO items = itemsMapper.getItemById(itemId);
        List<ItemsImgDO> itemsImgList = itemsImgMapper.getItemImgList(itemId);
        List<ItemsSpecDO> itemsSpecList = itemsSpecMapper.getItemSpecList(itemId);
        ItemsParamDO itemsParam = itemsParamMapper.getItemParam(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(itemsImgList);
        itemInfoVO.setItemSpecList(itemsSpecList);
        itemInfoVO.setItemParams(itemsParam);
        return itemInfoVO;
    }

    @Override
    public ItemCommentLevelCountVO getItemCommentCount(String itemId) {
        Integer totalCount = itemsCommentsMapper.getItemCommentCount(itemId, null);
        Integer goodCount = itemsCommentsMapper.getItemCommentCount(itemId, 1);
        Integer normalCount = itemsCommentsMapper.getItemCommentCount(itemId, 2);
        Integer badCount = itemsCommentsMapper.getItemCommentCount(itemId, 3);
        return new ItemCommentLevelCountVO(totalCount, goodCount, normalCount, badCount);
    }

    @Override
    public PagedGridResult<ItemCommentVO> getItemComments(String itemId, Integer level, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<ItemCommentVO> itemComments = itemsCommentsMapper.getItemComments(itemId, level);
        itemComments.parallelStream().forEach(ic -> ic.setNickname(DesensitizationUtil.commonDisplay(ic.getNickname())));
        PageInfo<ItemCommentVO> pageInfo = new PageInfo<>(itemComments);
        PagedGridResult<ItemCommentVO> pagedGridResult = new PagedGridResult<>();
        pagedGridResult.setPage(page);
        pagedGridResult.setRows(pageInfo.getList());
        pagedGridResult.setTotal(pageInfo.getPages());
        pagedGridResult.setRecords(pageInfo.getTotal());
        return pagedGridResult;
    }

    @Override
    public PagedGridResult<ItemSearchVO> searchItems(String keywords, String sort, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<ItemSearchVO> itemSearchVOS = itemsMapper.searchItems(keywords, sort);
        PageInfo<ItemSearchVO> pageInfo = new PageInfo<>(itemSearchVOS);
        PagedGridResult<ItemSearchVO> pagedGridResult = new PagedGridResult<>();
        pagedGridResult.setPage(page);
        pagedGridResult.setRows(pageInfo.getList());
        pagedGridResult.setTotal(pageInfo.getPages());
        pagedGridResult.setRecords(pageInfo.getTotal());
        return pagedGridResult;
    }

    @Override
    public ItemsSpecDO queryItemSpecById(String itemSpecId) {
        return itemsSpecMapper.queryItemSpecById(itemSpecId);
    }

    @Override
    public ItemsDO queryItemById(String itemId) {
        return itemsMapper.getItemById(itemId);
    }

    @Override
    public String queryItemMainImgById(String itemId) {
        return itemsImgMapper.queryItemMainImgById(itemId);
    }

    /**
     * 减少库存
     *
     * @param itemSpecId
     * @param buyCounts
     * @return
     */
    @Override
    public void decreaseItemSpecStock(String itemSpecId, Integer buyCounts) {
        itemsSpecMapper.decreaseItemSpecStock(itemSpecId, buyCounts);
    }
}
